package cn.wqk.rrtf.service;

import cn.wqk.rrtf.mapper.MemberMapper;
import cn.wqk.rrtf.mapper.RoleMapper;
import cn.wqk.rrtf.mapper.UserMapper;
import cn.wqk.rrtf.pojo.Member;
import cn.wqk.rrtf.pojo.Role;
import cn.wqk.rrtf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Transactional
public class MemberServiceImpl implements MemberService{
    @Autowired
    MemberMapper memberMapper;
    UserMapper userMapper;
    @Override
    public Integer insertMember(Member member, User user) throws DataAccessException{
        try {
            memberMapper.insertMember(member);
            Integer memberId=member.getMemberId();
            user.setUserId(memberId);
            userMapper.insertUser(user);
            return memberId;
        }catch (DataAccessException e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            System.out.println(e);
            return -1;
        }
    }

    @Override
    public Integer updateMember(Member member) throws DataAccessException{
            memberMapper.updateMember(member);
            Integer memberId=member.getMemberId();
            return memberId;
    }

    @Override
    public Integer deleteMember(Integer memberId) {
        Integer i=memberMapper.deleteMember(1);
        System.out.println(i+"删除会员成功！");
        return i;
    }

    @Override
    public List<Member> selectAllMember(Member member) {
        List<Member> memberList = memberMapper.selectAllMember();
        for (Member member1 : memberList) {
            System.out.println(member1);
        }
        return memberList;
    }

    @Override
    public List<Member> selectMemberByCondition(Member member) {
        List<Member> memberList = memberMapper.selectMemberByCondition(member);
        for (Member member1 : memberList) {
            System.out.println(member1);
        }
        return memberList;
    }

    @Override
    public List<Member> selectMemberByCourseId(Integer courseId) {
        List<Member> memberList = memberMapper.selectMemberByCourseId(courseId);
        return memberList;
    }

    @Override
    public List<Member> selectMemberByChatId(Integer chatId) {
        List<Member> memberList = memberMapper.selectMemberByChatId(chatId);
        return memberList;
    }

    @Override
    public List<Member> selectMemberByGradeId(Integer gradeId) {
        List<Member> memberList = memberMapper.selectMemberByGradeId(gradeId);
        return memberList;
    }
}
